package 生产者和消费者多对多;

import lombok.SneakyThrows;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: yangwk
 * @date: 2023/5/4
 * @description:
 **/
public class 生产者消费者 {
    static ReentrantLock reentrantLock = new ReentrantLock();
    static Thread[] threads = new Thread[2];

    static   public void produce() throws InterruptedException {
        reentrantLock.lock();
        System.out.println(Thread.currentThread().getName() + Thread.currentThread().getState().toString());
//        Thread.sleep(1000);
        reentrantLock.unlock();
    }

    public static void main(String[] args) throws InterruptedException {

        Thread thread = new Thread(new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true) {
//                    Thread.sleep(100);
                    System.out.println("daemon" + threads[0].getName() + threads[0].getState());
                    System.out.println("daemon" + threads[1].getName() + threads[1].getState());
                }

            }
        });
        for (int i = 0; i < threads.length; i++) {
            threads[i] = new Thread(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    produce();
                }
            });
            threads[i].start();
        }

        thread.setDaemon(true);
        thread.start();
        Thread.currentThread().join(4000);
    }

}
